
#include "config.h"
#include "app_config.h"
#include "app.h"
#include "debug.h"

#include "drv_rtc.h"


#if (TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G)||(TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB)
/**
 * @brief   UTC时间转为指定时区的时间
 * @param   utc_time:UTC时间结构体
 * @param   timezone: 时区 如：8（东8区） -8（西8区）
 * @param   local_time: 转换后的时间结构体
*/
void UTC_to_ZoneTime(const calendar_obj* utc_time, int timezone, calendar_obj* local_time)
{
	int year, month, day, hour;
    int lastday;                //last day of this month.
    int lastlastday;            //last day of last month.
 
    year    = utc_time->year;   //utc time.
    month   = utc_time->mon;
    day     = utc_time->date;
    hour    = utc_time->hour + timezone; 
 
    /* Step0: Based on the current month, calculate the number of days in the current and last month */
    if (1==month || 3==month || 5==month || 7==month || 8==month || 10==month || 12==month) {
        lastday = 31;
        lastlastday = 30;
        if (3 == month) {
            if ((0 == year%400) || ((0 == year%4) && (year%100 != 0))) { //if this is lunar year.
                lastlastday = 29;
            } else {
                lastlastday = 28;
            }
        } else if ((1 == month) || (8 == month)) {
            lastlastday = 31;
        }
    } else if (4==month || 6==month || 9==month || 11==month) {
        lastday = 30;
        lastlastday = 31;
    } else {
        lastlastday = 31;
        if ((0 == year%400) || ((0 == year%4) && (year%100 != 0))) {
            lastday = 29;
        } else {
            lastday = 28;
        }
    }
 
    /* Step1: Based on the current time zone, calculate the local time */
    if (hour >= 24) {           // if >24, day+1
        hour -= 24;
        day += 1; 
 
        if (day > lastday) {    // next month,  day-lastday of this month
            day -= lastday;
            month += 1;
 
            if (month > 12) {   //	next year , month-12
                month -= 12;
                year += 1;
            }
        }
    } else if (hour < 0) {      // if <0, day-1
        hour += 24;
        day -= 1;
 
        if (day < 1) {          // month-1, day=last day of last month
            day = lastlastday;
            month -= 1;
 
            if (month < 1) {    // last year , month=12
                month = 12;
                year -= 1;
            }
        }
    }
 
    /* Step2: Output the local_time */
	local_time->year  = year;
	local_time->mon = month;
	local_time->date  = day;
	local_time->hour  = hour;
	local_time->min	 = utc_time->min;
	local_time->sec	 = utc_time->sec;
}
#endif

/**
 * @brief   字符串转HEX
 * @param   input:输入字符串
 * @param   output:输出HEX字符串
 * @param   len: 输入字符串长度
 * @return  无
*/
void string_to_hex(const char *input, char *output, int len) 
{
    int i=0;
    for (i = 0; i < len && input[i] != '\0'; i++) {
        sprintf(output + (i * 2), "%02X", input[i]);
    }
    output[i*2] = '\0'; // 确保输出字符串以null结尾
}


char *strstr(char *str1, char *str2)
{
    char *p1 = str1;
    char *p2 = str2;
    char *p3 = NULL;
    while(*p1 != '\0')
    {
        p3 = p1;
        while(*p2 != '\0' && *p1 == *p2)
        {
            p1++;
            p2++;
        }

        if(*p2 == '\0')
            return p3;
        p1 = p3 + 1;
        p2 = str2;
    }
    return NULL;
}

/**
 * @brief   HEX转字符串
 * @param   input:输入HEX
 * @param   output:输出字符串
 * @param   len: 输入HEX长度
 * @return  无
*/
void hex_to_string(const char* input, char *output, int len)
{
    int i=0;
    int j=0;
    for (i=0,j=0; i<len; i+=2, j++) {
        uint16_t temp=0;
        sscanf(input + i, "%2x", &temp);
        sprintf(output + j, "%c", (char)temp);
        // output[j] = (uint8_t)temp;
    }
    output[j] = '\0';
}

unsigned long utf8_to_unicode(const char *utf8_str) 
{
    unsigned char *str = (unsigned char *)utf8_str;
    long i = 0;
 
    // while (str[i] != '\0') {
        unsigned long unicode_char = 0;
 
        // 判断当前字节的前缀，确定字符的字节数
        if ((str[i] & 0x80) == 0) {
            // 1字节字符 (ASCII字符)
            unicode_char = str[i];
            i++;
        } else if ((str[i] & 0xE0) == 0xC0) {
            // 2字节字符
            unicode_char = (str[i] & 0x1F) << 6;
            unicode_char |= (str[i+1] & 0x3F);
            i += 2;
        } else if ((str[i] & 0xF0) == 0xE0) {
            // 3字节字符
            unicode_char = (str[i] & 0x0F) << 12;
            unicode_char |= (str[i+1] & 0x3F) << 6;
            unicode_char |= (str[i+2] & 0x3F);
            i += 3;
        } else if ((str[i] & 0xF8) == 0xF0) {
            // 4字节字符
            unicode_char = (str[i] & 0x07) << 18;
            unicode_char |= (str[i+1] & 0x3F) << 12;
            unicode_char |= (str[i+2] & 0x3F) << 6;
            unicode_char |= (str[i+3] & 0x3F);
            i += 4;
        }
 
        // 输出 Unicode 字符（16进制）
        // log_d("Unicode: U+%X\n", unicode_char);
        
    // }
        return unicode_char;
}


// 将一个 Unicode 码点转换为 UTF-8 编码
int unicode_to_utf8(unsigned long unicode, unsigned char *utf8_str)
{
    if (unicode <= 0x7F) {
        // 1字节字符（ASCII范围）
        utf8_str[0] = (unsigned char)unicode;
        return 1;
    } else if (unicode <= 0x7FF) {
        // 2字节字符
        utf8_str[0] = (unsigned char)((unicode >> 6) | 0xC0);  // 110xxxxx
        utf8_str[1] = (unsigned char)((unicode & 0x3F) | 0x80);  // 10xxxxxx
        return 2;
    } else if (unicode <= 0xFFFF) {
        // 3字节字符
        utf8_str[0] = (unsigned char)((unicode >> 12) | 0xE0);  // 1110xxxx
        utf8_str[1] = (unsigned char)(((unicode >> 6) & 0x3F) | 0x80);  // 10xxxxxx
        utf8_str[2] = (unsigned char)((unicode & 0x3F) | 0x80);  // 10xxxxxx
        return 3;
    } else if (unicode <= 0x10FFFF) {
        // 4字节字符
        utf8_str[0] = (unsigned char)((unicode >> 18) | 0xF0);  // 11110xxx
        utf8_str[1] = (unsigned char)(((unicode >> 12) & 0x3F) | 0x80);  // 10xxxxxx
        utf8_str[2] = (unsigned char)(((unicode >> 6) & 0x3F) | 0x80);  // 10xxxxxx
        utf8_str[3] = (unsigned char)((unicode & 0x3F) | 0x80);  // 10xxxxxx
        return 4;
    } else {
        // 不支持的 Unicode 范围
        return -1;
    }
}
